(module-name <console.scm.action>)
(require <console.scm.manager>)
(require <console.scm.console>)

(define (action-init)
	(console.ActionSet:init))
(define (action-update task-id name state)
  (console.ActionSet:update name state))
(define (action-properties properties :: console.ActionProperties l)
  (for-each (lambda (p) (properties:put (car p) (cadr p))) l))
(define (action-add action)
	(console.ActionSet:add action))
(define (action-refresh)
	(console.ActionSet:refresh))
(define (make-action-property)
  (console.ActionProperties))
(define (make-action command name . arg)
  (define-class <aaction> (<console.ConsoleAction>)
  	((*init*
       (properties :: console.ActionProperties))
     (invoke-special <console.ConsoleAction> (this) '*init* properties))
    ((actionPerformed e :: java.awt.event.ActionEvent) :: <void> (send-command command (this) (string-append name " "))))
  (let ((action :: console.ConsoleAction (if (> (length arg) 1) (<aaction> (cadr arg)) (<aaction> (console.ActionProperties)))))
    (action:putValue (javax.swing.AbstractAction:.NAME) (name:toString))
    (if (> (length arg) 0)
      (action:putValue (javax.swing.AbstractAction:.MNEMONIC_KEY) (java.lang.Integer (as <int> (car arg)))))
    (action:putValue "action" action)
    action))
(define (make-setter-action converter validator short-cut id-name default . args)
  (let ((func (lambda (task-id action)
                (let* ((default-setter-prompter (lambda () 
                                           (javax.swing.JOptionPane:showInputDialog frame (string-append "enter " id-name ":") id-name (javax.swing.JOptionPane:.PLAIN_MESSAGE))))
                       (new-value (if (> (length args) 0) ((car args)) (default-setter-prompter))))
                  (if (eq? #!null new-value)
                    (task-state-info task-id "set cancelled")
                    (let ((value (converter new-value)))
                      (if (validator value)
                        (begin
                          (action:putValue id-name value)
                          (task-state-info task-id (string-append "set to " (if (number? value) (number->string value) value))))
                        (task-state-info task-id "invalid entry"))))))))
   (let ((action (make-action func id-name short-cut)))
    (action:addPropertyChangeListener (integerupdater action id-name))
    (action:putValue id-name default)
    action)))
(define (make-string-setter-action short-cut id-name default)
  (make-setter-action (lambda (x) x) (lambda (x) #t) short-cut id-name default))
(define (make-integer-setter-action short-cut id-name default)
  (make-setter-action string->number integer? short-cut id-name default))

